package com.yn.service.zy.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.yn.common.model.zy.InspectionOrderDO;
import com.yn.common.page.BasePage;
import com.yn.common.request.sc.PageParamRequest;
import com.yn.service.dao.zy.InspectionOrderMapper;
import com.yn.service.zy.InspectionOrderService;
import com.yn.service.zy.model.dto.InspectionOrderDTO;
import com.yn.service.zy.model.query.InspectionOrderQuery;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: InspectionOrderServiceImpl
 * @Description: 检查项订单ServiceImpl
 * @Author: ym
 * @Date: 2025/5/10 16:08
 **/
@Service
public class InspectionOrderServiceImpl extends ServiceImpl<InspectionOrderMapper, InspectionOrderDO> implements InspectionOrderService {

    @Resource
    private InspectionOrderMapper inspectionOrderMapper;

    @Override
    public IPage<InspectionOrderDTO> listPage(InspectionOrderQuery query, BasePage basePage) {
        IPage<InspectionOrderDO> page = new Page(basePage.getPage(),basePage.getLimit());
        LambdaQueryWrapper<InspectionOrderDO> queryWrapper = new LambdaQueryWrapper<>();
        IPage<InspectionOrderDO> inspectionOrderDOIPage = inspectionOrderMapper.selectPage(page, queryWrapper);
        return inspectionOrderDOIPage.convert(InspectionOrderDTO::transform);
    }
    @Override
    public BasePage<InspectionOrderDTO> listPage1(InspectionOrderQuery query, PageParamRequest pageRequest) {
        PageHelper.startPage(pageRequest.getPage(), pageRequest.getLimit());
        LambdaQueryWrapper<InspectionOrderDO> queryWrapper = new LambdaQueryWrapper<>();
        List<InspectionOrderDO> orderDOList = baseMapper.selectList(queryWrapper);
        if(CollUtil.isEmpty(orderDOList)){
            return BasePage.restPage(new ArrayList<>());
        }
        BasePage<InspectionOrderDO> pageList = BasePage.restPage(orderDOList);
        ArrayList<InspectionOrderDTO> list = new ArrayList<>(orderDOList.size());
        for (InspectionOrderDO inspectionOrderDO : orderDOList) {
            InspectionOrderDTO transform = InspectionOrderDTO.transform(inspectionOrderDO);
            list.add(transform);
        }
        BasePage<InspectionOrderDTO> inspectionOrderDTOBasePage = BasePage.restPage(list);
        BeanUtils.copyProperties(pageList, inspectionOrderDTOBasePage,"list");
        return inspectionOrderDTOBasePage;
    }

    @Override
    public InspectionOrderDTO getInspectionOrderDetail(String orderNo) {
        LambdaQueryWrapper<InspectionOrderDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(InspectionOrderDO::getInspectionOrderNo, orderNo);
        InspectionOrderDO inspectionOrderDO = inspectionOrderMapper.selectOne(queryWrapper);
        return InspectionOrderDTO.transform(inspectionOrderDO);
    }


}
